<template>
  <el-form
    :model="addForm"
    :rules="addFormRules"
    ref="addFormRef"
    label-width="70px"
  >
    <el-form-item label="用户名" prop="username">
      <el-input v-model="addForm.username"></el-input>
    </el-form-item>

    <el-form-item label="密码" prop="password" type>
      <el-input v-model="addForm.password"></el-input>
    </el-form-item>

    <el-form-item label="邮箱号" prop="email">
      <el-input v-model="addForm.email"></el-input>
    </el-form-item>

    <el-form-item label="手机" prop="mobile">
      <el-input v-model="addForm.mobile"></el-input>
    </el-form-item>
  </el-form>
</template>

<script>
import { checkEmail, checkMobile } from 'common/rule'
import { addUserInfo } from 'network/user'
export default {
  name: 'AddUser',
  data() {
    return {
      addForm: {
        username: '',
        password: '',
        email: '',
        mobile: '',
      },
      addFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '用户名的长度在3~10个字符之间',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '用户名的长度在6~15个字符之间',
            trigger: 'blur',
          },
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' },
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: checkMobile,
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    clearUserData() {
      this.$refs.addFormRef.resetFields()
    },
    confirmInfo() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return
        const res = await addUserInfo(this.addForm)
        if (res.meta.status !== 201) {
          this.$message.error('添加用户失败')
          return
        }
        this.$message.success('添加用户成功')
        this.$emit('getUserList')
        this.$bus.$emit('success')
      })
    },
  },
}
</script>
<style lang="less" scoped>
</style>